package com.iss.apartmentmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iss.apartmentmanage.entity.CheckinRecord;
import com.iss.apartmentmanage.entity.RoomManage;
import com.iss.apartmentmanage.mapper.CheckMapper;
import com.iss.apartmentmanage.mapper.CheckinRecordMapper;
import com.iss.apartmentmanage.mapper.RoomManagerMapper;
import com.iss.apartmentmanage.model.BaseModel;
import com.iss.apartmentmanage.model.RecordWithRoomForAll;
import com.iss.apartmentmanage.service.CheckService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@Service
public class CheckServiceImpl implements CheckService {
    @Autowired
    CheckMapper checkMapper;
    @Autowired
    RoomManagerMapper roomManagerMapper;
    @Autowired
    CheckinRecordMapper checkinRecordMapper;
    @Override
    public List<RecordWithRoomForAll> selectRoomForCheckOut(Integer pageNum, Integer pageSize, String roomType,String roomName,
                                                     Integer areaId,Double priceFrom,Double priceTo,Integer status) {

        List<RecordWithRoomForAll> recordWithRoomForAll=checkinRecordMapper.selectRoomForCheckOut(pageNum,pageSize,roomType,roomName,
                areaId, priceFrom, priceTo, status);


//        Page<CheckinRecord> page = new Page<>(pageNum,pageSize);
//        IPage<CheckinRecord> iPage = null;
//        QueryWrapper<CheckinRecord> wrapper= new QueryWrapper();
//
//
//        if(StringUtils.isNotEmpty(baseModel.getRoomName())) {
//            wrapper.eq("room_name", baseModel.getRoomName());
//        }
//        if(StringUtils.isNotEmpty(baseModel.getRoomType())){
//            wrapper.eq("room_type", baseModel.getRoomType());
//        }
//        if(baseModel.getPriceFrom()!=null&& baseModel.getPriceTo()!=null){
//            wrapper.between("room_price", baseModel.getPriceFrom(), baseModel.getPriceTo());
//        }
//        if(baseModel.getBelongAreaId()!=null){
//            wrapper.eq("area_id", baseModel.getBelongAreaId());
//        }
//        if(baseModel.getStatus()!=null){
//            wrapper.eq("status", baseModel.getStatus());
//        }
//        iPage =checkinRecordMapper.selectPage(page,wrapper);
        return recordWithRoomForAll;
    }

    /**
     * 办理退房
     * @param checkinRecord
     * @return
     */
    @Override
    public Integer updateRoomForFree(CheckinRecord checkinRecord) {
        int update=0;
        int update2=0;
        if(checkinRecord.getId()!=0){
            update = checkinRecordMapper.updateById(checkinRecord);
            update2 = roomManagerMapper.updateIsDeleteByRoomIds(checkinRecord.getStatus(),checkinRecord.getId());
            System.out.println(update2);
        }
        return update;
    }

    /**
     * 查询未入住的房间
     */

    public IPage<RoomManage> selectRoomForCheckIn(@RequestParam int pageNum, @RequestParam int pageSize,
                                                @RequestBody BaseModel baseModel){
       Page<RoomManage> page = new Page<>(pageNum,pageSize);
       IPage<RoomManage> iPage = null;
       QueryWrapper<RoomManage> wrapper= new QueryWrapper();
       if(baseModel.getBelongAreaId()!=null) {
           wrapper.eq("area_id", baseModel.getBelongAreaId());
       }
        if(StringUtils.isNotEmpty(baseModel.getRoomName())) {
            wrapper.eq("room_name", baseModel.getRoomName());
        }
        if(StringUtils.isNotEmpty(baseModel.getRoomType())){
            wrapper.eq("room_type", baseModel.getRoomType());
        }
        if(baseModel.getPriceFrom()!=null&& baseModel.getPriceTo()!=null){
            wrapper.between("room_price", baseModel.getPriceFrom(), baseModel.getPriceTo());
        }
        if(baseModel.getBelongAreaId()!=null){
            wrapper.eq("area_id", baseModel.getBelongAreaId());
        }
        if(baseModel.getStatus()!=null){
            wrapper.eq("status", baseModel.getStatus());
        }
       iPage =checkMapper.selectPage(page,wrapper);
       return iPage;


    }























}
